﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Business.Chk;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Mst;

namespace ESOL.WinUI.Qc
{
    public partial class frmManageQcSampingParameters : BaseForm.frmBaseMdiBusinessForm
    {
        #region Variable
        private QcSamplingParametersEntity mSelectedSamplingParameters;
        #endregion

        #region Constructor
        public frmManageQcSampingParameters()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void Init()
        {
            btnModify.Enabled = false;
            btnSubmit.Enabled = false;
            btnApprove.Enabled = false;
            btnDelete.Enabled = false;
        }
        private void SetButton()
        {
            if (mSelectedSamplingParameters == null)
                return;
            if (mSelectedSamplingParameters.Status.Equals("STATUS01"))
            {
                btnModify.Enabled = true;
                btnSubmit.Enabled = true;
                btnApprove.Enabled = false;
                btnDelete.Enabled = true;
            }
            if (mSelectedSamplingParameters.Status.Equals("STATUS02"))
            {
                btnModify.Enabled = false;
                btnSubmit.Enabled = false;
                btnApprove.Enabled = true;
                btnDelete.Enabled = false;
            }
            if (mSelectedSamplingParameters.Status.Equals("STATUS03"))
            {
                btnModify.Enabled = false;
                btnSubmit.Enabled = false;
                btnApprove.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (mSelectedSamplingParameters.Status.Equals("STATUS04"))
            {
                btnModify.Enabled = true;
                btnSubmit.Enabled = true;
                btnApprove.Enabled = false;
                btnDelete.Enabled = false;
            }
            if (mSelectedSamplingParameters.Status.Equals("STATUS05"))
            {
                btnModify.Enabled = false;
                btnSubmit.Enabled = false;
                btnApprove.Enabled = false;
                btnDelete.Enabled = false;
            }
        }
        private void BindComobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "STATUS", true);
            DataTable dt = MstCommonItemBs.GetMstCommonItemDtByType("STATUS");
            repositoryItemGridLookUpEdit1.DataSource = dt;
            repositoryItemGridLookUpEdit1.DisplayMember = "ITEM_NAME";
            repositoryItemGridLookUpEdit1.ValueMember = "ITEM_CODE";
        }
        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtName.Text) == false)
            {
                sCondition += " and NAME LIKE '%" + txtName.Text + "%' ";
            }
            if (cmbStatus.SelectedValue != null && string.IsNullOrEmpty(cmbStatus.SelectedValue.ToString()) == false)
            {
                sCondition += " and STATUS='" + cmbStatus.SelectedValue.ToString() + "' ";
            }
            else
            {
                sCondition += " and STATUS!='STATUS05' ";
            }
            return sCondition;
        }
        private void BindGrid()
        {
            string sCondition = GetCondition();
            DataTable dt = QcSamplingParametersBs.GetDtAllByCondition(sCondition, txtRowCount.Text, "");
            gvMain.DataSource = dt;
        }
        #endregion

        private void frmManageQcSampingParameters_Load(object sender, EventArgs e)
        {
            Init();
            BindComobox();
            BindGrid();
        }
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr = gridView1.GetDataRow(e.RowHandle);
            mSelectedSamplingParameters = new QcSamplingParametersEntity(dr);
            SetButton();
        }
        protected override void Click_Query()
        {
            BindGrid();
            //base.Click_Query();
        }
        protected override void Click_New()
        {
            //base.Click_New();
            frmMaintainQcSampingParameters frm = new frmMaintainQcSampingParameters();
            frm.ThisActionType = ActionType.New;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mSelectedSamplingParameters == null)
                return;
            frmMaintainQcSampingParameters frm = new frmMaintainQcSampingParameters(mSelectedSamplingParameters);
            frm.ThisActionType = ActionType.Modify;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
            //base.Click_Modify();
        }
        protected override void Click_Delete()
        {
            if (mSelectedSamplingParameters == null)
                return;
            mSelectedSamplingParameters.Status = "STATUS05";
            if (QcSamplingParametersBs.UpdateQcSamplingParameters(mSelectedSamplingParameters))
            {
                MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindGrid();
            }
            else
            {
                MessageBox.Show("删除失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //base.Click_Delete();
        }
        protected override void Click_Submit()
        {
            if (mSelectedSamplingParameters == null)
                return;
            mSelectedSamplingParameters.Status = "STATUS02";
            if (QcSamplingParametersBs.UpdateQcSamplingParameters(mSelectedSamplingParameters))
            {
                MessageBox.Show("送审成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                BindGrid();
            }
            else
            {
                MessageBox.Show("送审失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            //base.Click_Submit();
        }
        protected override void Click_Approve()
        {
            if (mSelectedSamplingParameters == null)
                return;
            frmMaintainQcSampingParameters frm = new frmMaintainQcSampingParameters(mSelectedSamplingParameters);
            frm.ThisActionType = ActionType.Approve;
            if (frm.ShowDialog() == DialogResult.OK)
            {
                BindGrid();
            }
            //base.Click_Approve();
        }


    }
}